Implementing advanced caching

ABSTRACT

Embodiments are disclosed for implementing a priority queue in a storage device, e.g., a solid state drive. At least some of the embodiments can use an in-memory set of blocks to store items until the block is full, and commit the full block to the storage device. Upon storing a full block, a block having a lowest priority can be deleted. An index storing correspondences between items and blocks can be used to update priorities and indicated deleted items. By using the in-memory blocks and index, operations transmitted to the storage device can be reduced.

CROSS-REFERENCES TO RELATED APPLICATIONS

This application is a continuation of commonly assigned U.S. patentapplication Ser. No. 14/316,395, filed on Jun. 26, 2014, and entitled“IMPLEMENTING ADVANCED CACHING,” the disclosure of which is herebyincorporated herein in its entirety by reference.

BACKGROUND

Some web sites store many documents and a subset of these documents isaccessed frequently. For example, social network web sites can storebillions of photographs, and a subset of these photographs can be veryfrequently accessed for an initial period of time (e.g., soon after thephotograph is first “shared” with friends) and then rarely accessedthereafter. Web site operators, e.g., social network web sites, canemploy caching techniques to reduce the overhead associated withaccessing these documents, but must manage a tradeoff between havingvery many web site servers to enable quick access to these documents andlarge caches to reduce load on the servers. These tradeoffs can affectspeed and cost. For example, caching can be done using relatively moreexpensive hardware, e.g., solid state drives (SSDs) instead of diskdrives to provide faster speeds.

An example of a caching algorithm is a “least recently used” (LRU)cache. An LRU cache discards the least recently used items first. Forexample, a photograph that is popularly accessed may remain in a cachefor an extended period, whereas a photograph that is accessed rarely maybe discarded from the cache. Other advanced caching algorithms alsoexist. The LRU algorithm can keep an initially posted photograph in thecache and then eventually discard the cache as the photograph becomesless frequently accessed.

Priority queues can be used to implement various advanced cachingalgorithms. A priority queue is a data type that is like a regular queueor stack data structure, but where additionally each element has apriority associated with it. To implement an LRU algorithm using apriority queue, priorities of items in the queue can be updated when theitems are accessed. Thus, an item that is accessed infrequently wouldeventually have a low priority and so may be discarded from the queue.On the other hand, an item that is accessed frequently will eventuallyhave a high priority and so may remain in the queue longer than otheritems.

When using SSDs to store data, it is generally desirable to minimize thenumber of write operations to reduce an undesirable operation referredto as “write amplification.” Write amplification occurs because, unlikewith magnetic disks, data in memory must be erased before it isrewritten. This writing and rewriting can cause data to be moved in theSSD, which results in many more writes than the initial write operation.Thus, when an application changes the priority of an item in a priorityqueue, the SSD may perform multiple writes. It is desirable to reducethe number of writes because SSDs (and memory generally) can have alimited write lifetime. Moreover, it can be advantageous to avoidoperations that prevent efficient use SSDs.

BRIEF DESCRIPTION OF THE DRAWINGS

Embodiments disclosed herein may be better understood by referring tothe following Detailed Description in conjunction with the accompanyingdrawings, in which like reference numerals indicate identical orfunctionally similar elements:

FIG. 1 is a block diagram illustrating an environment in which thetechnology may operate in various embodiments.

FIG. 2 is a block diagram illustrating items that may be stored in amemory or a solid state drives in various embodiments.

FIG. 3A is a table diagram illustrating data that may be stored in anindex, consistent with various embodiments.

FIG. 3B is another table diagram illustrating data that may be stored inan index, consistent with various embodiments.

FIG. 4 is a flow diagram illustrating a routine for implementing apriority queue in a storage device, consistent with various embodiments.

FIGS. 5A-D are portions of the flow diagrams illustrating variousaspects for implementing a priority queue, consistent with variousembodiments.

DETAILED DESCRIPTION

Embodiments are disclosed for implementing advanced caching, e.g., usingpriority queues, in conjunction with one or more storage devices.Examples of storage devices are solid state drives (SSDs) of variouscapacities, pluggable cards with flash memory, etc. In variousembodiments, one or more storage devices are divided into multiplelogical “blocks.” The logical blocks can be large enough to store itemsthat are to be tracked in a priority queue or cache, but much smallerthan the overall size of the storage device. A small subset of thelogical blocks may be identified as “active” blocks. Each active blockcan have a corresponding block that is stored in a memory region of acomputing device that implements the cache. A caching system can providean application program interface that applications can use to cacheitems, e.g., photographs, text, etc. As an example, when a socialnetworking web site stores or retrieves a photograph, it may invoke thecaching system to perform the storing or retrieving.

Each active block can have associated therewith a priority value. Insome embodiments, the active blocks are distributed so that the numberof items in adjacent active blocks is roughly equal. In anabsolute-priority example, if there are 10 items with priorities between1 and 5, 20 items with priorities 6 between and 10, and 10 items withpriorities between 11 and 15, and the caching system makes four blocksactive, it may select as priorities 3, 7, 9, and 13 so that there areapproximately 10 items in each of the four active blocks. In arelative-priority example, the priority numbers can be real numbers inthe range from 0 to 1. For example, the priority number 0.1 canrepresent a lower priority than the priority number 0.8. The cachingsystem can process these relative-priorities similarly to theabsolute-priorities as discussed above.

When an application adds an item to the cache, the application caninvoke the application program interface provided by the caching system.The caching system can store the item in an in-memory blockcorresponding to an active block in the storage device. The cachingsystem can also add an entry into an in-memory index to indicate inwhich active block the item was stored. The caching system adds the itemto the active block that has a priority that is closest to the addeditem. In case of a tie (e.g., the priority is exactly between twopriorities of active blocks), the caching system can add the item toeither block, e.g., a block having fewer items than the other block.When an item having the lowest priority is deleted, the caching systemmarks the item in the in-memory index as having been deleted, but doesnot delete the item in the storage device. When an item is read, thecaching system looks up its location (e.g., block identifier) in thein-memory index and then reads the item from the storage device.

The index can store information about items stored in all blocks,whether the blocks are active or not. In some embodiments, the indexstores information only about items stored in non-active blocks. Theinformation can include, for each item, its name, location (e.g.,logical block), priority, etc. When an in-memory block becomes full(e.g., can accommodate no more items), the caching system can commit thenow full block to the storage device and erase an older block in thestorage device. In some embodiments, the active block with the lowestpriority is erased. Before erasing the block in the storage device, thecaching system first reads that block into memory. The caching systemthen discards all items in that block that are marked in the in-memoryindex as having been deleted and removes entries in the index for thoseitems. The caching system then begins populating a new block in memoryand assigns a new active block in the storage device. In someembodiments, the new active block is assigned a priority that minimizesthe maximum difference between priority values for all active blocks. Asan example, if there are four blocks with priorities 3, 7, 9, and 15,the caching system may assign a priority of 12 to the new block tominimize the maximum difference between 9 and 15. The remaining items inthe erased block that are not indicated in the index as having beendeleted are then inserted into the active blocks closest to thepriorities of those items. In relative-priority embodiments, the cachingsystem can assign a priority to a block so as to minimize the maximumnumber of items between priorities. For example, if there are fourblocks with priorities 0.1, 0.3, 0.5 and 0.9, the caching system mayassign a priority of 0.8 to a new block to minimize the maximum numberof items between priorities (e.g., there are three groups in a firstrelative-priority range of 0 to 0.5, but there is only one group in asecond relative-priority range of 0.51 to 1). Similarly, the remainingitems in the erased block that are not indicated in the index as havingbeen deleted are then inserted into the active blocks closest to thepriorities of those items.

When an item's priority is increased, e.g., because it is read, thecaching system updates the priority value in the in-memory index, butdoes not modify its value in the storage device. By postponing writingto the storage device, write-related issues (e.g., bandwidth and writeamplification) are reduced. Moreover, because blocks are larger thanitems, the buffered writes improve the efficiency of using storagedevices (e.g., SSDs) for priority queues.

In the foregoing and following descriptions, although SSDs may be usedfor convenience of description, it is to be understood that other solidstate memory-based storage devices can also be employed.

Several embodiments of the described technology are described in moredetail in reference to the Figures. The computing devices on which thedescribed technology may be implemented may include one or more centralprocessing units, memory, input devices (e.g., keyboard and pointingdevices), output devices (e.g., display devices), storage devices (e.g.,disk drives), and network devices (e.g., network interfaces). The memoryand storage devices are computer-readable storage media that may storeinstructions that implement at least portions of the describedtechnology. In addition, the data structures and message structures maybe stored or transmitted via a data transmission medium, such as asignal on a communications link. Various communications links may beused, such as the Internet, a local area network, a wide area network,or a point-to-point dial-up connection. Thus, computer-readable mediacan comprise computer-readable storage media (e.g., “non-transitory”media) and computer-readable transmission media.

FIG. 1 is a block diagram illustrating an environment 100 in which thetechnology may operate in various embodiments. The environment 100 canbe a portion of a computing device, e.g., a general-purpose computingdevice or a special purpose computing device. The environment 100 caninclude a processor 102, e.g., a central processing unit, a memory 104,and one or more solid state drives (SSDs) 106 a, 106 b, and 106 n. Invarious embodiments, multiple such environments 100 may be deployed,e.g., as web server computing devices, caching system computing devices,etc.

FIG. 2 is a block diagram illustrating items that may be stored in amemory 104 or a solid state drive 106 a in various embodiments. Thememory 104 can have a blocks region 202 and an index region 203. Theblocks region 202 can store one or more in-memory blocks, e.g., 202 a,202 b, 202 c, and 202 m. The index region 203 can store an index ofstored items, as further described below.

The illustrated SSD 106 a can have an active blocks region 204 and aninactive blocks region 206. The active blocks region 204 can store oneor more blocks that are also cached in memory 104. For example, block204 a stored in the SSD 106 a can correspond to block 202 that is storedin memory 104. In various embodiments, some or all blocks in the activeblocks region 204 can be cached in the blocks region 202 of memory 104.Each block in active blocks region 204 can have associated therewith apriority. For example, block 204 a has priority 0.1, block 204 b haspriority 0.2, block 204 c has priority 0.3, block 204 d has priority0.4, and block 204 m has priority 1.0. The inactive blocks region 206can store blocks that are not presently cached in memory 104.

In various embodiments, each block is larger than items to be stored inthe priority queue (e.g., in a cache), but much smaller than the totalspace available in the SSD. For example, there may be hundreds of blocksin an SSD. In some embodiments, the number of active blocks may bebetween 10 and 30.

FIG. 3A is a table diagram illustrating data that may be stored in anindex, consistent with various embodiments. The index can indicate aname 302 for each item, a priority 304 associated with the item, anindication 306 of whether the item is deleted, and a block number 308(e.g., logical or physical block numbers) of the items stored. Row 310indicates that an item with name “name 1” has a priority of 0.05, is notdeleted, and is stored at block 204 a; row 312 indicates that an itemwith name “name 2” has a priority of 0.35, is not deleted, and is storedat block 204 c; row 314 indicate that an item with name “name 3” has apriority of 0.12, has been deleted, and is stored at block 204 a; androw 316 indicates that an item with name “name 5” has a priority of0.99, is not deleted, and is stored at block 204 m. In variousembodiments, items can be stored in blocks having nearest priorities.For example, item 310 can be stored in block 204 a because block 204 ahas a priority of 0.1 whereas block 204 b has a priority of 0.2, and thepriority for item 310 is 0.05, which is closer mathematically to 0.1than to 0.2.

FIG. 3B is another table diagram illustrating data that may be stored inan index, consistent with various embodiments. In the illustratedembodiment, the index can indicate a name 302 for each item, anindication 306 of whether the item is deleted, and a virtual blocknumber 318 (e.g., a logical block number) of the items stored. Comparingto the embodiments described in FIG. 3A, the index described here doesnot include a priority column and only includes virtual block numbers(i.e., no physical block number). By doing so, the index described inFIG. 3B can have a smaller size than the embodiments described in FIG.3A. In various embodiments, system administrators can determine whichinformation should be included in the index table, depending on variousfactors, such as availability of computing resources and/or systemefficiency.

While FIGS. 3A and 3B and each of the table diagrams discussed belowshow a table whose contents and organization are designed to make themmore comprehensible by a human reader, those skilled in the art willappreciate that actual data structures used by the facility to storethis information may differ from the table shown, in that they, forexample, may be organized in a different manner; may contain more orless information than shown; may be compressed and/or encrypted; etc.

FIG. 4 is a flow diagram illustrating a routine 400 for implementing apriority queue in a storage device, consistent with various embodiments.The routine 400 begins at block 402. At block 404, the routine 400selects a specified number (M) of blocks. The selected blocks areindicated to be active blocks. At block 406, the routine 400 assignspriorities to the selected blocks. In various embodiments, thepriorities may be distributed evenly, distributed to approximatelybalance the number of items stored in each block, etc. At block 408, theroutine 400 receives a command (also referred to as an “operation”)relating to the priority queue, e.g., to write an item, read an item,delete an item, or change the priority of an item. At decision block410, the routine 400 determines the type of the received command. If thetype of the received command is “read,” the routine 400 continues atblock 412. If the type of the received command is “write,” the routine400 continues at block 414. If the type of the received command is“delete,” the routine 400 continues at block 416. If the type of thereceived command is “change priority,” the routine 400 continues atblock 418. The illustrated flow of these commands continues in FIGS.5A-D.

Those skilled in the art will appreciate that the logic illustrated inFIG. 4 and described above, and in each of the flow diagrams discussedbelow, may be altered in various ways. For example, the order of thelogic may be rearranged, substeps may be performed in parallel,illustrated logic may be omitted, other logic may be included, etc.

FIG. 5A illustrates a routine 500 to implement the “read” command. Atblock 501, the routine 500 looks up the item in the index to identifythe block at which the item is stored. As an example, the routine 500may look up the name of the item and identify a block at which the itemis stored. At block 502, the routine 500 reads the identified block fromthe SSD and returns the item at block 503. In alternate embodiments, theroutine 500 may simply read the item from the in-memory block.

FIG. 5B illustrates a routine 550 to implement the “write” command. Atblock 504, the routine 550 selects an active block with the closestpriority. As an example, if the item to be written has priority 31, theroutine 550 may select block 204 c illustrated in FIG. 2, because block204 c has a priority of 30, which is mathematically closer to 30 thanpriorities of the other blocks. At block 505, the routine 550 insertsthe item into the selected block in memory but not the SSD. At block506, the routine 550 adds to the index the name of the item, theindicated priority, and the SSD block identifier corresponding to thecached block in memory. At decision block 508, the routine 550determines whether the block is full. If the block is full, the routine550 continues at block 510. Otherwise, the routine 550 returns at block524. The routine 550 then commits the cached block to the SSD anddeletes the block with the lowest priority from the SSD. The logic forthis deletion will now be explained in further detail. At block 510, theroutine writes the now full block to the SSD. At block 512, the routine550 reads the block with the lowest priority to memory. At block 514,the routine 550 deletes the block that was read from the SSD. At block516, the routine creates a new active block. At block 518, the routine550 assigns a priority to the new active block. In various embodiments,the priority may be assigned to minimize the maximum difference betweenthe priority values for all active blocks. At block 519, the routine 550implicitly deletes all items in the block having a priority lower than athreshold X (which can be determined by a system administrator; e.g.,relative-priority 0.9). “Implicit deletion” can be defined as removingitems from the SSD at eviction time without updating these items'priorities after they are inserted into corresponding blocks. Forexample, in an LRU cache, all items that have not been accessed afterthey are inserted into blocks can be implicitly deleted at evictiontime. In some embodiments, the implicit deletion process can be done bynot reinserting items to the block with the lowest priority (e.g.,lowest 10%). At block 520, the routine 550 removes from the index allcorresponding entries marked as deleted. At block 522, the routine 550inserts the remaining items into active blocks that have the closestpriority to the remaining items. The routine 550 then returns at block524.

FIG. 5C illustrates a routine 600 to implement the “delete” command(please refer back to FIG. 4 and corresponding descriptions above). Atblock 602, the routine 600 marks, in the index, the item as deleted, butdoes not indicate any change in the SSD. The routine 600 then returns atblock 604.

FIG. 5D illustrates a routine 700 to implement the “update priority”command (please refer back to FIG. 4 and corresponding descriptionsabove). At block 702, the routine 700 updates the priority for the itemin the index. The routine 700 then returns at block 704.

Although the subject matter has been described in language specific tostructural features and/or methodological acts, it is to be understoodthat the subject matter defined in the appended claims is notnecessarily limited to the specific features or acts described above.Rather, the specific features and acts described above are disclosed asexample forms of implementing the claims. Accordingly, the invention isnot limited except as by the appended claims.

We claim:
 1. A method for implementing a priority queue in a storagedevice, comprising: identifying a set of active blocks, each activeblock associated with an active-block priority, wherein the active-blockpriority is determined based on a number of a set of items to be storedand on a set of item priorities corresponding to the set of items to bestored; creating multiple in-memory blocks, each created in-memory blockcorresponding to one of the active blocks and having the active-blockpriority of the corresponding active block; receiving an item of the setof items to be stored in the storage device and a corresponding itempriority for the received item; adding, to an index, an entrycorresponding to the received item, the entry including the itempriority for the received item and an identification of a block at whichthe received item is to be stored; and storing the received item in anin-memory block having an active-block priority closest to the receiveditem.
 2. The method of claim 1, further comprising: creating a newactive block; assigning to the new active block a new active-blockpriority that minimizes a maximum difference between two values of theactive-block priority; reading into memory from the storage device ablock having a lowest active-block priority; deleting a read block fromthe storage device; removing from the index entries corresponding toitems in the read block that are indicated in the index as having beendeleted; and inserting into active blocks having a closest active-blockpriority items in the read block that are not indicated in the index ashaving been deleted.
 3. The method of claim 1, further comprisingreading from the storage device a requested item upon receiving a readcommand for the requested item.
 4. The method of claim 1, furthercomprising deleting in the index but not the storage device an item uponreceiving a delete command for the item.
 5. The method of claim 1,further comprising updating an item priority for an item in the indexbut not the storage device upon receiving a command to update the itempriority for the item.
 6. The method of claim 1, further comprisingproviding an application program interface configured to receive storagecommands and item priorities for stored items.
 7. A computer-readablestorage medium storing instructions for implementing a priority queue ina storage device, comprising: instructions for identifying a set ofactive blocks, each active block associated with an active-blockpriority, wherein the active-block priority is determined based on anumber of a set of items to be stored and on a set of item prioritiescorresponding to the set of items to be stored; instructions forcreating multiple in-memory blocks, each created in-memory blockcorresponding to one of the active blocks and having the active-blockpriority of the corresponding active block; instructions for receivingan item to be stored of the set of items in the storage device and acorresponding item priority for the received item; instructions foradding, to an index, an entry corresponding to the received item, theentry including the item priority for the received item and anidentification of a block at which the received item is to be stored;and instructions for storing the received item in an in-memory blockhaving an active-block priority closest to the received item.
 8. Thecomputer-readable storage medium of claim 7, further comprising:instructions for creating a new active block; instructions for assigningto the new active block a new active-block priority that minimizes amaximum difference between two values of the active-block priority;instructions for reading into memory from the storage device a blockhaving a lowest active-block priority; instructions for implicitlydeleting a read block from the storage device; instructions for removingfrom the index entries corresponding to items in the read block that areindicated in the index as having been deleted; and instructions forinserting into active blocks having a closest active-block priorityitems in the read block that are not indicated in the index as havingbeen deleted.
 9. The computer-readable storage medium of claim 7,further comprising instructions for reading from the storage device arequested item upon receiving a read command for the requested item. 10.The computer-readable storage medium of claim 7, further comprisinginstructions for deleting in the index but not the storage device anitem upon receiving a delete command for the item.
 11. Thecomputer-readable storage medium of claim 7, further comprisinginstructions for updating an item priority for an item in the index butnot the storage device upon receiving a command to update the itempriority for the item.
 12. The computer-readable storage medium of claim7, further comprising instructions for providing an application programinterface configured to receive storage commands and item priorities forstored items.
 13. A system, comprising: a storage device; a set ofactive blocks, each active block being associated with an active-blockpriority, wherein the active-block priority is determined based on anumber of a set of items to be stored and on a set of item prioritiescorresponding to the set of items to be stored; a memory having an indexand in-memory blocks corresponding to the active blocks; and a componentconfigured to receive an item of the set of items to be stored in thestorage device and a corresponding item priority for the received item,add to the index an entry corresponding to the received item, the entryincluding the item priority for the received item and an identificationof a block at which the received item is to be stored, store thereceived item in an in-memory block having an active-block priorityclosest to the received item.
 14. The system of claim 13, furthercomprising: a component configured to create a new active block, assignto the new active block a new active-block priority that minimizes amaximum difference between two values of the active-block priority, readinto memory from the storage device a block having a lowest active-blockpriority, delete a read block from the storage device, remove from theindex entries corresponding to items in the read block that areindicated in the index as having been deleted, and insert into activeblocks having a closest active-block priority items in the read blockthat are not indicated in the index as having been deleted.
 15. Thesystem of claim 13, further comprising a component configured to readfrom the storage device a requested item upon receiving a read commandfor the requested item.
 16. The system of claim 13, further comprising acomponent configured to delete in the index but not the storage devicean item upon receiving a delete command for the item.
 17. The system ofclaim 13, further comprising a component configured to update an itempriority for an item in the index but not the storage device uponreceiving a command to update the item priority for the item.
 18. Thesystem of claim 13, further comprising a component configured to providean application program interface configured to receive storage commandsand item priorities for stored items.
 19. The system of claim 13,wherein the storage device includes a solid state drive, and whereineach of the active blocks is larger than the items to be stored in thestorage device.
 20. The system of claim 13, wherein the storage deviceincludes a memory-based storage device.